/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package photoshare.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import photoshare.dto.PhotoDTO;
import photoshare.util.DBUtil;

/**
 *
 * @author PhotoShare
 */
public class PhotoDAO {
    
    private String READ_BY_ALL = "SELECT * FROM PHOTOS";
    private String READ_BY_ID = "SELECT * FROM PHOTOS WHERE PhotoID = ?";
    private String CREATE = "INSERT INTO PHOTOS VALUES(?,?,?)";
    private String DELETE_BY_ID = "UPDATE PHOTOS SET IsACTIVE= 0 WHERE PhotoID = ?";
    private String UPDATE_BY_ID = "UPDATE PHOTOS SET PASS = ? WHERE PhotoID = ?";

    private Connection conn = null;
    private PreparedStatement pst = null;

    public PhotoDAO() {
    }

    public boolean create(PhotoDTO photoDTO) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(CREATE);
            pst.setString(1, photoDTO.getPath());
            pst.setString(2, photoDTO.getStatus());
            pst.setLong(3, photoDTO.getView());
            pst.setString(4, photoDTO.getDescription());
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public ArrayList<PhotoDTO> readAll() {
        try {
            ArrayList<PhotoDTO> arr = null;
            arr = new ArrayList<PhotoDTO>();
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ALL);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                PhotoDTO dto = new PhotoDTO();
                dto.setPath(rs.getString(2));
                dto.setStatus(rs.getString(3));
                dto.setView(rs.getInt(4));
                dto.setDescription(rs.getString(5));
                arr.add(dto);
            }
            return arr;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public PhotoDTO readById(Integer photoId) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ID);
            pst.setInt(1, photoId);
            //pst = conn.prepareStatement("select * from acc WHERE accountID = 'admin'");
            ResultSet rs = pst.executeQuery();
            PhotoDTO photoDTO = new PhotoDTO();
            while (rs.next()) {
                photoDTO.setPath(rs.getString(2));
                photoDTO.setStatus(rs.getString(3));
                photoDTO.setView(rs.getInt(4));
                photoDTO.setDescription(rs.getString(5));
            }
            return photoDTO;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public boolean updateById(PhotoDTO photoDTO, Integer photoId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(UPDATE_BY_ID);
            //Tham so can Update trong cau lenh SQL
            pst.setString(1, photoDTO.getPath());
            //Tham so dieu kien cua cau lenh Update
            pst.setInt(2, photoId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public boolean deleteById(Integer photoId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(DELETE_BY_ID);
            //Tham so dieu kien cho cau lenh Update
            pst.setInt(1, photoId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }


}
